﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Hosting;
using System.Web.Script.Serialization;

/// <summary>
/// Pub 的摘要说明
/// </summary>
public class Pub
{

    /// <summary>
    /// Json 字符串 转换为 DataTable数据集合
    /// </summary>
    /// <param name="json"></param>
    /// <returns></returns>
    public static DataTable ToDataTableTwo(string json)
    {
        DataTable dataTable = new DataTable();  //实例化
        DataTable result;
        try
        {
            JavaScriptSerializer javaScriptSerializer = new JavaScriptSerializer();
            javaScriptSerializer.MaxJsonLength = Int32.MaxValue; //取得最大数值
            ArrayList arrayList = javaScriptSerializer.Deserialize<ArrayList>(json);
            if (arrayList.Count > 0)
            {
                foreach (Dictionary<string, object> dictionary in arrayList)
                {
                    if (dictionary.Keys.Count == 0)
                    {
                        result = dataTable;
                        return result;
                    }
                    //Columns
                    if (dataTable.Columns.Count == 0)
                    {
                        foreach (string current in dictionary.Keys)
                        {
                            dataTable.Columns.Add(current, dictionary[current].GetType());
                        }
                    }
                    //Rows
                    DataRow dataRow = dataTable.NewRow();
                    foreach (string current in dictionary.Keys)
                    {
                        dataRow[current] = dictionary[current];
                    }
                    dataTable.Rows.Add(dataRow); //循环添加行到DataTable中
                }
            }
        }
        catch
        {
        }
        result = dataTable;
        return result;
    }

    #region base64转图片
    /// <summary>
    /// 图片上传 Base64转图片
    /// </summary>
    /// <param name="Base64">Base64数据</param>
    /// <param name="imgName">图片名字</param>
    /// <returns>返回一个相对路径</returns>
    public static string decodeBase64ToImage(string Base64, string imgName)
    {

        string filename = "";//声明一个string类型的相对路径

        string base64 = Base64.Substring(Base64.IndexOf(",") + 1);      //将‘，’以前的多余字符串删除
        System.Drawing.Bitmap bitmap = null;//定义一个Bitmap对象，接收转换完成的图片
        //string tmpRootDir = HttpContext.Current.Server.MapPath(HttpContext.Current.Request.ApplicationPath.ToString()); //获取程序根目录 
        try//会有异常抛出，try，catch一下
        {

            byte[] arr = Convert.FromBase64String(base64);//将纯净资源Base64转换成等效的8位无符号整形数组

            MemoryStream ms = new MemoryStream(arr);//转换成无法调整大小的MemoryStream对象
            bitmap = new System.Drawing.Bitmap(ms);//将MemoryStream对象转换成Bitmap对象

            filename = imgName + ".jpg";//所要保存的相对路径及名字
            string url = HttpRuntime.AppDomainAppPath.ToString();
            string tmpRootDir = HttpContext.Current.Server.MapPath(HttpContext.Current.Request.ApplicationPath.ToString()); //获取程序根目录 E:\赚钱项目\按键云平台_改_青春不迷茫\
            string imagesurl2 = tmpRootDir + @"Upfile\Images\" + filename; //转换成绝对路径 
            bitmap.Save(imagesurl2, System.Drawing.Imaging.ImageFormat.Jpeg);//保存到服务器路径
                                                                             //bitmap.Save(filePath + ".bmp", System.Drawing.Imaging.ImageFormat.Bmp);
                                                                             //bitmap.Save(filePath + ".gif", System.Drawing.Imaging.ImageFormat.Gif);
                                                                             //bitmap.Save(imagesurl2, System.Drawing.Imaging.ImageFormat.Png);
            ms.Close();//关闭当前流，并释放所有与之关联的资源
            bitmap.Dispose();
        }
        catch (Exception ex)
        {
            return "异常：出错啦，错误详情=>" + ex.Message;
        }
        return filename;//返回相对路径
    }
    #endregion


    /// <summary>
    /// 项目目录
    /// </summary>
    /// <returns></returns>
    public static string GetRootPath()
    {
        string AppPath = "";
        HttpContext HttpCurrent = HttpContext.Current;
        HttpRequest Req;
        if (HttpCurrent != null)
        {
            Req = HttpCurrent.Request;

            AppPath = Req.ApplicationPath;
        }
        return AppPath;
    }
    /// <summary>
    /// 项目目录绝对路径
    /// </summary>
    /// <returns></returns>
    public static string GetRootPathAbsolute()
    {
        string absolutePath = string.Empty;
        try
        {
            absolutePath = HostingEnvironment.MapPath(GetRootPath());
        }
        catch { }
        return absolutePath;
    }

    /// <summary>
    /// 项目目录绝对路径
    /// </summary>
    /// <returns></returns>
    public static string GetRootPathAbsolute(string virturePath)
    {
        string absolutePath = string.Empty;
        try
        {
            absolutePath = HostingEnvironment.MapPath(virturePath);
        }
        catch { }
        return absolutePath;
    }

}